import 'dart:math';

import 'package:flutter/widgets.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_gifimage/flutter_gifimage.dart';
import 'package:flutter/material.dart'
    hide RefreshIndicator, RefreshIndicatorState;

/*
  I use my plugin to implements gif effect,this plugin can help you to controll gif easily,
  see page to find about usage: (https://github.com/peng8350/flutter_gifimage)
*/
class GifPullHeader extends RefreshIndicator {
  GifPullHeader() : super(height: 50.0, refreshStyle: RefreshStyle.Follow);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return GifPullHeaderState();
  }
}

class GifPullHeaderState extends RefreshIndicatorState<GifPullHeader>
    with SingleTickerProviderStateMixin {
  GifController _gifController;

  String mingYan = "下拉刷新";
  int rnIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    // init frame is 2
    _gifController = GifController(
      vsync: this,
      value: 1,
    );
    super.initState();
    var rng = new Random();
    rnIndex = rng.nextInt(49);
  }

  @override
  void onModeChange(RefreshStatus mode) {
    // TODO: implement onModeChange
    if (mode == RefreshStatus.refreshing) {
      _gifController.repeat(
          min: 0, max: 23, period: Duration(milliseconds: 2000));
    }
    super.onModeChange(mode);
  }

  @override
  Future<void> endRefresh() {
    var rng = new Random();
    rnIndex = rng.nextInt(49);
    _gifController.value = 0;
    return _gifController.animateTo(23, duration: Duration(milliseconds: 2000));
  }

  @override
  void resetValue() {
    // TODO: implement resetValue
    // reset not ok , the plugin need to update lowwer
    _gifController.value = 0;
    super.resetValue();
  }

  @override
  Widget buildContent(BuildContext context, RefreshStatus mode) {
    // TODO: implement buildContent
    mingYan = mingYan.length > 20 ? mingYan.substring(0, 20) + ".." : mingYan;

    return new Container(
        child: new Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          GifImage(
            image: AssetImage("assets/images/refresh_loading.gif"),
            controller: _gifController,
            height: 30,
            width: 30,
          ),
          Text(
            mingYan,
            style: QuhuiConstant.smallSubText,
          )
        ],
      ),
    ));
    Row();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _gifController.dispose();
    super.dispose();
  }
}
